//
//  WBNavigationController.swift
//  微博
//
//  Created by Jiang on 2019/2/14.
//  Copyright © 2019 Jiang. All rights reserved.
//

/*
 1.隐藏默认的Bar
 2.统一处理push
 */


import UIKit

class WBNavigationController: UINavigationController {

    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的NavigationBar
        navigationBar.isHidden = true;
    }
    
    
    
    //重写push 方法,所有的push动作都会调用此方法
    //viewController 是被push的控制器,设置他的左侧的按钮作为返回按钮
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        print(viewController)
        //如果不是栈底控制器才需要隐藏,根控制器不需要处理
        if children.count > 0 {
            //隐藏底部的TabBar
            viewController.hidesBottomBarWhenPushed = true
            tabBarController?.tabBar.isTranslucent = false
            
            //判断控制器类型(有push的控制器之后才需要判断他)
            if let vc = viewController as? WBBaseViewController {
                
                var title = "返回"
                //判断控制器的级数,只有一个子控制器的时候,显示s栈底控制器的标题
                if children.count == 1 {
                    //显示首页的标题
                    title = children.first?.title ?? "返回" //取出第一个控制器的标题
                }else {
                    title = "返回"
                }
                
                //取出自定义的navItem,设置左侧按钮作为返回按钮
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title, target: self, action: #selector(popToParent),isBack:true)
            }
        }
        
        super.pushViewController(viewController, animated: true)
        
        
    }
    
    ///(按钮点击方法)POP 返回到上一级控制器
    @objc private func popToParent() {
        popViewController(animated: true)
    }
    
    
}
